package com.wswy.wzcx.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;



/**
 * an download progressbar
 * Created by wangduheng26 on 16/7/28.
 * wangduheng26@gmail.com
 */
public class FineProgressBar extends View  {

    private static final String TAG = "DownloadProgressBar";

    /**
     * the length of highlighted area
     */
    private static final float OVERLAY_SCALE = 0.48f;


    public static final String COLOR_PREFIX = "#";

    /**
     * the color of highlight area
     */
    public static final String HIGH_COLOR = COLOR_PREFIX + "66ffff";

    /**
     * the start color of gradient
     */
    public static final String HIGH_TRANSPARENT_COLOR = COLOR_PREFIX + "0066e5ff";

    /**
     * the color of progress
     */
    public static final String PROGRESS_COLOR = COLOR_PREFIX + "14cccc";

    /**
     * the color of background
     */
    public static final String BACKGROUND_COLOR = COLOR_PREFIX + "f0f0f0";

    /**
     * the width of the view
     */
    private int mWidth;

    /**
     * the height of the view
     */
    private int mHeight;

    /**
     * the progress of this progress bar
     */
    private float mProgress=80;

    /**
     * the offset of canvas
     */
    private int mTranslate;

    /**
     * the highlight paint
     */
    private Paint mGradientPaint = new Paint();


    private Paint mPaint = new Paint();

    private int mLastWidth = -1;

    private int mLastHeight = -1;

    /**
     * the length of highlight area
     */
    private int mGradientWidth;

    /**
     * whether the progress is suspended
     */
    private boolean mSuspend;

    private int mIncreaseOffset;


    public FineProgressBar(Context context) {
        this(context, null);
    }

    public FineProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FineProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }


    @Override
    protected void onDraw(Canvas canvas) {

        int progress = (int) (mWidth * mProgress);

        drawBackground(canvas, progress);
        drawHighLight(canvas, progress);

        if (!mSuspend && progress != 0) {
            invalidate(0, 0, progress, mHeight);
        }

    }


    /**
     * draw the background
     *
     * @param canvas
     * @param progress
     */
    private void drawBackground(Canvas canvas, int progress) {
        mPaint.setColor(Color.parseColor(BACKGROUND_COLOR));
        canvas.drawRect(progress, 0, mWidth, mHeight, mPaint);
    }


    /**
     * draw the highlight area
     *
     * @param canvas
     * @param progress
     */
    private void drawHighLight(Canvas canvas, int progress) {
        if (mProgress != 0) {
            if (mLastWidth != mWidth || mLastHeight != mHeight) {
                mLastWidth = mWidth;
                mLastHeight = mHeight;

                mIncreaseOffset = mWidth / 50;
                mGradientWidth = (int) (mWidth * OVERLAY_SCALE);

                Shader shader = new LinearGradient(0, 0, mGradientWidth, 0,
                        new int[]{Color.parseColor(PROGRESS_COLOR), Color.parseColor(HIGH_COLOR), Color.parseColor(PROGRESS_COLOR)},
                        new float[]{0, 0.5f, 1f}, Shader.TileMode.CLAMP);

                mGradientPaint.setShader(shader);

            }

            canvas.clipRect(0, 0, progress, mHeight);

            mTranslate = (mTranslate + mIncreaseOffset) % (progress + mGradientWidth);

            canvas.translate(mTranslate - mGradientWidth - progress, 0);

            mPaint.setColor(Color.parseColor(PROGRESS_COLOR));
            canvas.drawRect(0, 0, progress, mHeight, mPaint);

            canvas.translate(progress, 0);
            canvas.drawRect(0, 0, mGradientWidth, mHeight, mGradientPaint);

            canvas.drawRect(mGradientWidth, 0, progress + mGradientWidth, mHeight, mPaint);

        }
    }


    public void setProgress(float progress) {
        this.mProgress = progress;
        postInvalidate();
    }


    public void suspend() {
        mSuspend = true;
    }

    public void resume() {
        mSuspend = false;
        postInvalidate();
    }

    public void setStatus(boolean loading) {
        if (loading) {
            resume();
        } else {
            suspend();
        }

    }

    public void setVisible(boolean downloading) {
        if (downloading) {
            setVisibility(View.VISIBLE);
        } else {
            setVisibility(View.GONE);
        }
    }
}
